<?php

Loader::import('pspframework.utils.acl.AclInterface');

class IniAcl extends Object implements AclInterface {


	public $config = null;


	public function initialize(& $component) {
	}


	public function allow($aro, $aco, $action = "*") {
	}


	public function deny($aro, $aco, $action = "*") {
	}


	public function inherit($aro, $aco, $action = "*") {
	}


	public function check($aro, $aco, $action = null) {

		if ($this->config == null) {
			$this->config = $this->readConfigFile(CONFIGS . DS . 'acl.ini.php');
		}
		
		$aclConfig = $this->config;
		if (isset($aclConfig[$aro]['deny'])) {
			$userDenies = $this->arrayTrim(explode(",", $aclConfig[$aro]['deny']));
			
			if (array_search($aco, $userDenies)) {
				return false;
			}
		}

		if (isset($aclConfig[$aro]['allow'])) {
			$userAllows = $this->arrayTrim(explode(",", $aclConfig[$aro]['allow']));

			if (array_search($aco, $userAllows)) {
				return true;
			}
		}

		if (isset($aclConfig[$aro]['groups'])) {
			$userGroups = $this->arrayTrim(explode(",", $aclConfig[$aro]['groups']));

			foreach ($userGroups as $group) {
				if (array_key_exists($group, $aclConfig)) {
					if (isset($aclConfig[$group]['deny'])) {
						$groupDenies = $this->arrayTrim(explode(",", $aclConfig[$group]['deny']));

						if (array_search($aco, $groupDenies)) {
							return false;
						}
					}

					if (isset($aclConfig[$group]['allow'])) {
						$groupAllows = $this->arrayTrim(explode(",", $aclConfig[$group]['allow']));

						if (array_search($aco, $groupAllows)) {
							return true;
						}
					}
				}
			}
		}
		return false;
	}


	public function readConfigFile($filename) {
        Loader::import('pspframework.utils.reader.IniReader');
		$iniFile = new IniReader(dirname($filename) . DS);
		return $iniFile->read(basename($filename));
	}


	public function arrayTrim($array) {
		foreach ($array as $key => $value) {
			$array[$key] = trim($value,'/');
		}
		array_unshift($array, "");
		return $array;
	}

}
?>
